<?php
/** * Statistic_model Class *
 * @package TIAP
 * @subpackage Model
 * @category user
 * @author setyagus sucipto
 * @link http://twitter.com/agoes82
 * @description
 */
class Statistic_model extends MY_Model {
	var $name, $password, $description, $public, $path, $create_by, $creator_name, $logo,
	$network_table;

	function Statistic_model() {
		parent::MY_Model();
		//$this->setTableName('network');
		$this->user_table = 'user_tiab';
		$this->user_info_table = 'user_info';
		$this->network_table = 'network';
		$this->user_network_table = 'user_network';
		$this->user_settings_table = 'user_settings';
		$this->user_household_table = 'user_household';
		$this->user_political_bias_table = 'user_political_bias';
		$this->political_party_table = 'political_party';
	}

	/* method get_active_users
	 * @description    count all of the active user
	 * @parameter
	 * return INT
	 */
	function get_active_users_count(){
		$this->db->where('activation_status','active');
		return $this->db->count_all_results($this->user_table);
	}
	/* method get_active_users
	 * @description    count all of the active user
	 * @parameter
	 * return INT
	 */
	function get_active_users_count_by_gender($gender = 'M'){
		$this->db->select("{$this->user_info_table}.sex");
		$this->db->join($this->user_table,"{$this->user_table}.id = {$this->user_info_table}.id",'left');
		$this->db->where("{$this->user_info_table}.sex",$gender);
		return $this->db->count_all_results($this->user_info_table);
	}
	/* method get_active_users
	 * @description    sum all of the household info
	 * @parameter
	 * return INT
	 */
	function get_household_sum(){
		$this->db->select_sum("adult_count");
		$this->db->select_sum("children_count");
		$this->db->select_sum("pensioner_count");
		$query =  $this->db->get($this->user_household_table);
		if($query->num_rows() > 0){
	  return $query->row_array();
		}else{
	  return FALSE;
		}
	}
	function get_political_bias_stat(){
		$result = array();
		$total_pol = 0;
		$this->db->order_by('order_num','ASC');
		$query = $this->db->get($this->political_party_table);
		//echo $this->db->last_query();
		//echo '<br>';
		$political_list = $query->result_array();
		foreach($political_list as $key=>$value){
			$this->db->where('political_party_id',$value['id']);
			$jumlah_pol = $this->db->count_all_results($this->user_political_bias_table);
			//echo $this->db->last_query().'=>'.$jumlah_pol.$value['name'];
			//echo '<br>';
			$value['jumlah'] = $jumlah_pol;
			$result[$key] =  $value;
			$total_pol += $jumlah_pol;
		}
		foreach($result as $key=>$value){
			if($total_pol == 0){
				$result[$key]['jumlah_percentage'] = 0;
			}else{
				$result[$key]['jumlah_percentage'] = round(($value['jumlah'] / $total_pol *100),2);
			}
		} 
		return $result;
	}

}

/* End of file network_model.php */
/* Location: ./system/application/models/network_model.php */
